#include "StdAfx.h"
#include "MySocket.h"
#include "IPAddr.h"
MySocket::MySocket(void)
{
	activePage=0;
	isChange=false;
}
MySocket::~MySocket(void)
{
	delete serverIPString;
	delete myIPString;
	//delete inst;
}
MySocket* MySocket::inst;
bool MySocket::ServerConnect()
{

	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,2), &wsaData);

	//SOCKET cSocket;
	_connectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	BYTE byteBuf[4];
	//Server IP
	serverIPString = new char[15];

	CIPAddr serverIPAddr (127,0,0,1);
	serverIPAddr.ToChar(serverIPString);

	//Init connect Socket
	sockaddr_in sAddr;
	sAddr.sin_family = AF_INET;
	sAddr.sin_port = htons(5678);
	sAddr.sin_addr.S_un.S_addr = inet_addr(serverIPString);

	int iConnectResult = connect(_connectSocket,(sockaddr*)&sAddr,sizeof(sAddr));
	if (iConnectResult !=0)
	{
		return false;
	}

	//Tao thread lang nghe thong diep tu server
	AfxBeginThread(RecieveThread,this);
	return true;	
}

void MySocket::SendMessToServer( CMessage mes )
{
	char* buf = new char[300];
	mes.ToChar(buf);

	int len = strlen(buf);
	int i = send(_connectSocket,buf,len,0);
	delete buf;
}

